package org.cse.ui;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;

import org.cse.model.DocInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import ru.xpoft.vaadin.DiscoveryNavigator;
import ru.xpoft.vaadin.VaadinMessageSource;

import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@Component
@Scope("prototype")
/**
 * main entry for vaadin
 * @author jeremie
 *
 */
public class CseMainUI extends UI {

	public static final String PERSISTENCE_UNIT = "csePersistenceUnit";

	@Autowired
	private EntityManagerFactory emf;

	@Autowired
	private transient ApplicationContext applicationContext;

	@Autowired
	private VaadinMessageSource messageSource;

	@Override
	protected void init(final VaadinRequest request) {
		setSizeFull();

		// commebta
		Layout l = new VerticalLayout();
		setContent(l);
  
		l.addComponent(new Label(messageSource.getMessage("CseMainUI.welcome")));

		// *** TEST 2 ***
		DocInfo doc = new DocInfo();
		doc.setUrl("url1");
		doc.setComment("comment");

		doc.persist();
		DocInfo doc2 = new DocInfo();
		doc2.setUrl("url2");
		doc2.setComment("comment2");
		doc2.persist();

		JPAContainer<DocInfo> docInfoContainer = JPAContainerFactory.make(
				DocInfo.class, emf.createEntityManager());
		Table docInfoTable = new Table("DOCINFOS", docInfoContainer);
		l.addComponent(docInfoTable);

		// DiscoveryNavigator navigator = new DiscoveryNavigator(this, this);

		// Notification.show(String.format("Session counter: %d, application counter: %d",
		// sessionCounter.getCount(), applicationCounter.getCount()));
	}

}
